
import numpy as np
import matplotlib.pyplot as plt

###############################################

# read kinetics output
def filereader(fname):
    tempdat = {}
    kin = open(fname,'r')
    temp = kin.readline()
    while not 'EDDY' in temp:
        temp = kin.readline()
    temp = kin.readline()
    temp = kin.readline()
    temp = kin.readline().split()
    z, t, p, d, eddy = [],[],[],[],[]
    while len(temp)>1:
        z+= [float(temp[1])]
        t+=[float(temp[2])]
        d+= [float(temp[3])]
        p+=[float(temp[4])]
        eddy+=[float(temp[7])]
        temp = kin.readline().split()
    temp = kin.readline()
    while not 'MIX' in temp:
        temp = kin.readline()
    temp = kin.readline()
    header = kin.readline().split()
    while len(header)>1:
        for ix in header[1:]:
            tempdat[ix] = []
        temp = kin.readline().split()
        while len(temp)>1:
            for ix in range(1,len(header)):
                tempdat[header[ix]] += [float(temp[ix+1])]
            temp = kin.readline().split()
        header = kin.readline().split()
    tempdat['z'], tempdat['t'], tempdat['p'], tempdat['d'], tempdat['eddy'] = z,t,p,d,eddy
    return tempdat

############################################### 

# read input
dat = {}
infile = open('input.dat','r')
temp = infile.readline().split()
while len(temp) > 1:
    if not temp[1] in list(dat.keys()):
        dat[temp[1]] = {}
    dat[temp[1]][temp[3]] = filereader(temp[2])
    temp = infile.readline().split()

############################################### 

# create plot
fig,ax=plt.subplots(ncols=3,nrows=2,figsize=(12,8))
lns = [':','-.','--','-','*']
lw = 2.0
plot_dict = {}
for i in list(dat.keys()):
    jctr = 0
    plot_dict['ax'+str(i)] = ax[int(float(i)/3),int(float(i)%3)].twiny()
    for j in list(dat[i].keys()):
        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['O2'],dat[i][j]['z'],\
                                                     lns[int(jctr)],color='magenta',\
                                                     markersize=8,linewidth=lw,markevery=5)
        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['H2'],dat[i][j]['z'],\
                                                     lns[int(jctr)],color='blue',\
                                                     markersize=8,linewidth=lw,markevery=5)
        plot_dict['ax'+str(i)].semilogx(dat[i][j]['CO'],dat[i][j]['z'],\
                                        lns[int(jctr)],color='orange',\
                                        markersize=8,linewidth=lw,markevery=5)
        plot_dict['ax'+str(i)].semilogx(dat[i][j]['CO2'],dat[i][j]['z'],\
                                        lns[int(jctr)],color='green',\
                                        markersize=8,linewidth=lw,markevery=5)
#        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['CO'],dat[i][j]['z'],\
#                                                     lns[int(jctr)],color='orange',\
#                                                     markersize=8,linewidth=lw,markevery=5)
#        ax[int(float(i)/3),int(float(i)%3)].set_xlim([1e-8,1])
        plot_dict['ax'+str(i)].set_xlim([1e-2,1])
        jctr +=1
plt.savefig('mix.png') 


#fig,ax1=plt.subplots(figsize=(6,1))
#plt.subplots_adjust(bottom=0.25)
#ax1 = ax[0,0].inset_axes([0.1,0.9,0.1,0.2])
#ax1 = fig.add_axes([0.12,0.45,0.05,0.1])

#ax1 = ax[0,0].twiny()
#tlns = ['o','s','^','D','*']
#tsizes = [10,9,10,8,12]
#jctr = 0
#for j in list(dat[i].keys()):
#    ax1.plot(dat['0'][j]['t'][0],550,tlns[jctr],markersize=tsizes[jctr],color='black')
#             lns[int(jctr)],color='grey',\
#             markersize=8,linewidth=lw,markevery=5)
#    jctr +=1
#ax1.tick_params(axis='both', which='minor', labelsize=12)

#for ix in range(0,3):
#    for iy in range(0,2):
#        ax[iy,ix].set_ylim([-10,590])


plt.savefig('t.png',dpi=300)

    


